/****************************************************************************

 Copyright (C) 2009 Deepak Chandran
 Contact: Deepak Chandran (dchandran1@gmail.com)
 See COPYWRITE.TXT

This is an example application that uses the TinkerCell Core library
****************************************************************************/

#include "SimpleDesigner.h"
#include "NodeGraphicsItem.h"
#include <QRectF>
#include <QGraphicsRectItem>

#include <iostream>

using namespace Tinkercell;

SimpleDesigner::SimpleDesigner(): Tool(tr("Simple Designer"))
{
  actionGroup = new QActionGroup(this);
  mode = 0;
	
  toolBar = new QToolBar(this);
  actionGroup->setExclusive(true);
	
  arrowButton = new QAction(QIcon(":/images/arrow.png"),tr("arrow"),toolBar);
  QAction * nodeButton = new QAction(QIcon(":/images/blueRect.png"),tr("node"),toolBar);
  QAction * edgeButton = new QAction(QIcon(":/images/1to1.png"),tr("reaction"),toolBar);
  QAction * lineButton = new QAction(QIcon(":/images/pencil.png"), tr("line"), toolBar);
	
  arrowButton->setCheckable(true);
  nodeButton->setCheckable(true);
  edgeButton->setCheckable(true);
  lineButton->setCheckable( true );
  toolBar->addAction(arrowButton);
  toolBar->addAction(nodeButton);
  toolBar->addAction(edgeButton);
  toolBar->addAction( lineButton );

  connect(actionGroup, SIGNAL(triggered(QAction*)), this, SLOT(actionTriggered(QAction*)));
  actionGroup->addAction(arrowButton);
  actionGroup->addAction(nodeButton);
  actionGroup->addAction(edgeButton);
  actionGroup->addAction( lineButton );
	
  QGridLayout * layout1 = new QGridLayout;
  QGridLayout * layout2 = new QGridLayout;
  QVBoxLayout * layout3 = new QVBoxLayout;
	
  layout1->addWidget(new QLabel(tr("name")),0,0);
  layout1->addWidget(new QLabel(tr("concentration")),1,0);
  layout1->addWidget(name1 = new QLineEdit,0,1);
  layout1->addWidget(conc  = new QLineEdit,1,1);
	
  layout2->addWidget(new QLabel(tr("name")),0,0);
  layout2->addWidget(new QLabel(tr("rate")),1,0);
  layout2->addWidget(name2 = new QLineEdit,0,1);
  layout2->addWidget(rate  = new QLineEdit,1,1);
	
	
  listWidget = new QListWidget;
  layout3->addWidget(listWidget);
	
  groupBox1 = new QGroupBox(tr(" Species "));
  groupBox1->setLayout(layout1);
	
  groupBox2 = new QGroupBox(tr(" Reaction "));
  groupBox2->setLayout(layout2);
	
  groupBox3 = new QGroupBox(tr(" Parameters "));
  groupBox3->setLayout(layout3);
	
  QVBoxLayout * layout4 = new QVBoxLayout;
  layout4->addWidget(groupBox1);
  layout4->addWidget(groupBox2);
  layout4->addWidget(groupBox3);
	
  setLayout(layout4);
	
  groupBox1->hide();
  groupBox2->hide();
	
  connect(name1,SIGNAL(editingFinished()),this,SLOT(nameChanged()));
  connect(name2,SIGNAL(editingFinished()),this,SLOT(nameChanged()));
  connect(conc,SIGNAL(editingFinished()),this,SLOT(concentrationChanged()));
  connect(rate,SIGNAL(editingFinished()),this,SLOT(rateChanged()));
  line = NULL;
  cad_line = NULL;
}

void SimpleDesigner::nameChanged() 
{
  GraphicsScene * scene = currentScene();
  if (!scene || scene->selected().size() != 1) return;
	
  QGraphicsItem * selectedItem = scene->selected()[0];
  ItemHandle * handle = getHandle(selectedItem);
	
  if (name1->isVisible())
    scene->rename(handle,name1->text());
  else
    if (name2->isVisible())
      scene->rename(handle,name2->text());
		
  for (int i=0; i < handle->graphicsItems.size(); ++i)
    handle->graphicsItems[i]->update();
}

void SimpleDesigner::addParameters(QStringList& newVars)
{
  NetworkWindow * network = currentWindow();
  if (!network || !network->modelItem()) return;
	
  listWidget->clear();
	
  ItemHandle * modelItem = network->modelItem(); //handle for the entire model
	
  QStringList vars;
  DataTable<qreal> params;
	
  if (modelItem->hasNumericalData("parameters"))     
    {
      params = modelItem->data->numericalData["parameters"]; //get existing set of parameters
      vars = params.getRowNames();
    }
	
  for (int i=0; i < newVars.size(); ++i)
    if (!vars.contains(newVars[i]))
      {
	vars << newVars[i];
	params.value(newVars[i],0) = 1.0;   //add new parameters to existing set
      }
	
  modelItem->data->numericalData["parameters"] = params;   //update with new set of parameters
	
  vars.clear();
	
  for (int i=0; i < params.rows(); ++i)
    vars << params.rowName(i) + tr(" = ") + QString::number(params.value(i,0));  //"param = value"
	
  listWidget->addItems(vars);   //update list widget for viewing parameters
}

void SimpleDesigner::rateChanged() 
{
  NetworkWindow * win = currentWindow();
  if (!win) return;
	
  GraphicsScene * scene = win->scene;
  if (!scene || scene->selected().size() != 1) return;
	
  QGraphicsItem * selectedItem = scene->selected()[0];
  ItemHandle * handle = getHandle(selectedItem);
	
  if (!handle || !handle->hasTextData("rate")) return;
	
	
  QString formula = rate->text();
	
  //find all the new variables in this equation
  QStringList newVars;
  bool ok = win->parseMath(formula,newVars);
	
  if (ok)
    {
      DataTable<QString> table;
      table.value(0,0) = formula;
      scene->changeData(handle->name + tr("'s rate changed"), handle,"rate",&table);
      addParameters(newVars); //insert new variables into the modelItem
      setToolTip(handle);
    }
  else
    mainWindow->statusBar()->showMessage(tr("error in formula : ") + formula);
}

void SimpleDesigner::concentrationChanged()
{
	GraphicsScene * scene = currentScene();
	if (!scene || scene->selected().size() != 1) return;
	
	QGraphicsItem * selectedItem = scene->selected()[0];
	ItemHandle * handle = getHandle(selectedItem);
	
	if (!handle || !handle->hasNumericalData("concentration")) return;
	
	DataTable<qreal> table;
	
	bool ok;
	
	table.value(0,0) = conc->text().toDouble(&ok);
	
	scene->changeData(handle->name + tr("'s concentration changed"), handle,"concentration",&table);
}

void SimpleDesigner::actionTriggered(QAction* action)
{
	mode = 0;
	
	if (!action) {
	  //	  currentScene()->am->step( e_esc );
	  mainWindow->console()->message( "actionTriggered: !action" );
	  return;
	}
		
	if (action->text() == tr("node")){
		mode = 1;

	  if( currentScene()->work_mode == Tinkercell::GraphicsScene::mycad_mode ){
	    currentScene()->work_mode = Tinkercell::GraphicsScene::tinkercell_mode;
	    mainWindow->console()->message( "actionTriggered: turn tinkercell_mode" );
	  }else{
	    currentScene()->work_mode = Tinkercell::GraphicsScene::mycad_mode;
	    mainWindow->console()->message( "actionTriggered: turn mycad_mode" );

	  }

	}else if (action->text() == tr("reaction")){
	  mode = 2;
	}
	else if ( action->text() == tr( "line" )){
	  mode = 3; //draw line
	  mainWindow->console()->message( "start point:" );
	  currentScene()->am->step( e_start );
	  currentScene()->am->step( e_begin_line );
	  currentScene()->work_mode = Tinkercell::GraphicsScene::mycad_mode;
	  currentScene()->useDefaultBehavior = false;
	  if( currentScene()->work_mode == Tinkercell::GraphicsScene::mycad_mode ){
	    mainWindow->console()->message( "actionTriggered: turn mycad_mode" );
	  }else{
	    mainWindow->console()->message( "actionTriggered: turn tinkercell_mode" );
	  }
	}else if ( action->text() == tr( "arrow" )){
	  mainWindow->console()->message( "actionTriggered: turn ArrowButton" );
	  currentScene()->am->step( e_esc );
	  mode = 0;
	}
}
	
bool SimpleDesigner::setMainWindow(MainWindow * main)
{
  Tool::setMainWindow(main);
  if (mainWindow)
    {
      mainWindow->addToolBar(Qt::LeftToolBarArea,toolBar);

      setWindowTitle(tr("Information Box"));
      setWindowIcon(QIcon(tr(":/images/about.png")));
      QDockWidget * dockWidget = mainWindow->addToolWindow(this, MainWindow::DockWidget, Qt::BottomDockWidgetArea, Qt::NoDockWidgetArea);
      if (dockWidget)
	dockWidget->setFloating(true);
		
      connect(mainWindow,SIGNAL(itemsInserted(NetworkWindow*, const QList<ItemHandle*>&)),
	      this, SLOT(itemsInserted(NetworkWindow*,const QList<ItemHandle*>&)));
				
      connect(mainWindow,
	      SIGNAL(itemsSelected(GraphicsScene *, const QList<QGraphicsItem*>&, QPointF, Qt::KeyboardModifiers)),
	      this,
	      SLOT(itemsSelected(GraphicsScene *, const QList<QGraphicsItem*>&, QPointF, Qt::KeyboardModifiers))
	      );
				
      connect(mainWindow,
	      SIGNAL(escapeSignal(const QWidget * )),
	      this,
	      SLOT(escapeSignal(const QWidget * ))
	      );
		
      connect(mainWindow,
	      SIGNAL(mousePressed(GraphicsScene * , QPointF , Qt::MouseButton, Qt::KeyboardModifiers)),
	      this,
	      SLOT(mousePressed(GraphicsScene * , QPointF , Qt::MouseButton, Qt::KeyboardModifiers))
	      );
      
      connect(mainWindow,
	      SIGNAL( mouseMoved(GraphicsScene * , QGraphicsItem*, QPointF, Qt::MouseButton, Qt::KeyboardModifiers, QList<QGraphicsItem*>& )),
	      this,
	      SLOT( mouseMoved(GraphicsScene * , QGraphicsItem*, QPointF, Qt::MouseButton, Qt::KeyboardModifiers, QList<QGraphicsItem*>& ))
	      );
      

      return true;
    }
  return false;
}

void SimpleDesigner::mousePressed(GraphicsScene * scene, QPointF point, Qt::MouseButton, Qt::KeyboardModifiers modifiers)
{
  if (!scene || !scene->networkWindow || mode == 0) return;

  switch( mode ){
  case 1:
    {
  QGraphicsItem * item = new SimpleNode;
  NodeHandle * handle = new NodeHandle;
	
  QList<QString> names = scene->networkWindow->symbolsTable.handlesFirstName.keys();
  names += scene->networkWindow->symbolsTable.dataRowsAndCols.keys();
	
  int i = 1;
  handle->name = tr("s1");
	
  while (names.contains(handle->name))
    {
      ++i;
      handle->name = tr("s") + QString::number(i);
    }
	
  setHandle(item,handle);
  item->setPos(point);
	
  scene->insert(handle->name + tr(" inserted"),item);
    }
  break;
  case 2:
    break;
  case 3: //draw line
    {
      if( !line ){
//	currentScene()
	//Non stop line
	mainWindow->console()->message( "next point:" );
	this->line = new QGraphicsLineItem( QLineF( point, point ));
	cad_line = new LineItem( QLineF( point, point ), NULL, scene);
	//	cad_line = new LineItem( 0 );
	cad_line->setPen( QPen( QColor(0, 0, 0,255 )));
	cad_line->setLine( QLineF( point, point ));
	cad_line->setFlag( QGraphicsItem::ItemIsSelectable, false );

	NodeHandle * line_handle = new NodeHandle;
	
	QList<QString> line_names = scene->networkWindow->symbolsTable.handlesFirstName.keys();
	line_names += scene->networkWindow->symbolsTable.dataRowsAndCols.keys();
	
	int i = 1;
	line_handle->name = tr("l1");
	
	while ( line_names.contains( line_handle->name )){
	  ++i;
	  line_handle->name = tr("l") + QString:: number(i);
	}
	//	setHandle( line, line_handle );
	setHandle( cad_line, line_handle );
	scene->insert(line_handle->name + tr(" inserted"),cad_line);
	//  	scene->addItem( cad_line );
	/*
	NodeGraphicsItem::ControlPoint * control_point = new NodeGraphicsItem::ControlPoint();
	control_point->setPos( point );
	scene->addItem( control_point );
	*/
	//QGraphicsRectItem * rect = new QGraphicsRectItem( * control_point->rect );
	//setHandle( rect, line_handle );
	//scene->insert(line_handle->name + tr("control poit inserted"),rect);
	//node.addControlPoint( rect );

	//	QPaint * p = scene->getPaint();
	//mainWindow->console()->message( "SimpleDesigner::mousePressed::inser Control Point" );

	//line = NULL;
      }else{ //line
	QLineF newLine( cad_line->QGraphicsLineItem::line().p1(), point );
	//	line->setLine( newLine );
	//	line->setFlag( QGraphicsItem::ItemIsSelectable, true );
	//	line->setFlag( QGraphicsItem::ItemIsMovable, true );

	cad_line->setLine( newLine );
	cad_line->setFlag( QGraphicsItem::ItemIsSelectable, true );
	cad_line->setFlag( QGraphicsItem::ItemIsMovable, true );
	cad_line->QGraphicsLineItem::setPen( QPen( QColor(0, 0, 0,255) ));
	QGraphicsLineItem * lineitem = qgraphicsitem_cast<QGraphicsLineItem*>(cad_line);
	if ( lineitem ){
	  mainWindow->console()->message( "SimpleDesigner::mousePressed: this cast line" );
	}else{
	  mainWindow->console()->message( "SimpleDesigner::mousePressed: this no cast line" );
	}
	line = NULL;
	cad_line = NULL;
       	mainWindow->console()->message( "start point:" );
      }
    }
    break;
  default:
    break;
  }
	
}
void SimpleDesigner::mouseMoved(GraphicsScene *scene, QGraphicsItem *item , QPointF point, Qt::MouseButton, Qt::KeyboardModifiers modifiers, QList<QGraphicsItem*>& list_items){
  if (!scene || !scene->networkWindow || !line ) return;
  //  mainWindow->console()->message( "mouse move to next point:" );
  switch( mode ){
  case 3:{
    QLineF newLine( cad_line->QGraphicsLineItem::line().p1(), point );
    //    line->setLine( newLine );
    //    line->setFlag( QGraphicsItem::ItemIsSelectable, true );
    //    line->setFlag( QGraphicsItem::ItemIsMovable, true );
    cad_line->setLine( newLine );
    cad_line->setPen( QPen( QColor(0, 0, 0,255 )));
    cad_line->setFlag( QGraphicsItem::ItemIsSelectable, false );
    cad_line->setFlag( QGraphicsItem::ItemIsMovable, true );

    //    QLineF * tmp_line = new QLineF( line->line().p1(), point );
    //    line->setLine( *tmp_line );
    //    delete tmp_line;
  }
    break;
  case 4:{
    QLineF newLine( line->line().p1(), point );
    line->setLine( newLine );
  }
    break;
  default:
    break;
  }
}

void SimpleDesigner::setToolTip(ItemHandle* item)
{
  if (!item) return;
	
  if (NodeHandle::cast(item) && item->hasNumericalData("concentration")) //is a node
    {
      for (int i=0; i < item->graphicsItems.size(); ++i)
	item->graphicsItems[i]->setToolTip
	  (
	   item->name + tr(" : ") + QString::number(item->numericalData("concentration"))
	   );
    }
  else
    if (ConnectionHandle::cast(item) && item->hasTextData("rate")) //is a connection
      {
	for (int i=0; i < item->graphicsItems.size(); ++i)
	  item->graphicsItems[i]->setToolTip
	    (
	     item->name + tr(" : ") + item->textData("rate")
	     );
      }
}

void SimpleDesigner::itemsInserted(NetworkWindow * win,const QList<ItemHandle*>& items)
{
    mainWindow->console()->message( "inserted name" );
  for (int i=0; i < items.size(); ++i) {
 

    //    ItemHandle * nhdl = getHandle( items[i] );
   QString nm( items[i]->name );


    mainWindow->console()->message( nm );

    if (NodeHandle::cast(items[i])) { //is node?
      items[i]->numericalData("concentration") = 10.0;
      mainWindow->console()->message( "itemsInserted:" );

    }
    if (ConnectionHandle::cast(items[i])) {//is reaction?
      ConnectionHandle * connection = ConnectionHandle::cast(items[i]);
      QString rate;
			
      if (connection->hasTextData("rate")) { //rate already exists
	QStringList newVars;
	rate = connection->textData("rate");
	bool ok = win->parseMath(rate,newVars);
					
	if (ok){
	  addParameters(newVars);
	}
      }else {	
	QList<NodeHandle*> nodes = connection->nodesIn();
			
	rate = tr("1.0");
				
	for (int j=0; j < nodes.size(); ++j)
	  rate += tr(" * ") + nodes[j]->name;   //default mass-action rate
      }
      connection->textData("rate") = rate;
    }
    setToolTip(items[i]);
  }
}

void SimpleDesigner::deselectItem(GraphicsScene * scene, QGraphicsItem * item)
{
std::cout << "SimpleDesigner::deselectItem" << std::endl;
  selectItem(scene, item, false);
}

void SimpleDesigner::selectItem(GraphicsScene * scene, QGraphicsItem * item, bool select)
{
  std::cout << "SimpleDesigner::selectItem" << std::endl;
  
  if (NodeGraphicsItem::topLevelNodeItem(item)) {
    NodeGraphicsItem * node = NodeGraphicsItem::topLevelNodeItem(item);
    node->setBoundingBoxVisible(select);
    mainWindow->console()->message( "selectItem:" );
    if (select)
      for (int j=0; j < node->boundaryControlPoints.size(); ++j)
	scene->moving() += node->boundaryControlPoints[j];
  }else if (ConnectionGraphicsItem::topLevelConnectionItem(item)) {
    ConnectionGraphicsItem * connection = ConnectionGraphicsItem::topLevelConnectionItem(item);
    if (select) {
      connection->setPen(QPen(QColor(255,0,0,255)));
      scene->moving().removeAll(item);
      scene->moving() += connection->controlPointsAsGraphicsItems();
    }else {
      connection->setPen(connection->defaultPen);
      connection->update();
    }
    connection->setControlPointsVisible(select);     //show the red box
  }else if (qgraphicsitem_cast<NodeGraphicsItem::ControlPoint*>(item)){
    qgraphicsitem_cast<NodeGraphicsItem::ControlPoint*>(item)->nodeItem->setBoundingBoxVisible(select);
    mainWindow->console()->message( "selectItem show the red box:" );
  }else if (qgraphicsitem_cast<ConnectionGraphicsItem::ControlPoint*>(item)){
    qgraphicsitem_cast<ConnectionGraphicsItem::ControlPoint*>(item)->connectionItem->setControlPointsVisible(select);
    mainWindow->console()->message( "selectItem setControlPointsVisible:" );
  }

  ///////////////////////////////////////////////////////////////////////////
  QGraphicsLineItem * lineitem =  qgraphicsitem_cast<QGraphicsLineItem*>(item);
LineItem * line_item = qgraphicsitem_cast<LineItem*>(item);
  if( line_item ){
    mainWindow->console()->message( "SimpleDesigner::selectItem: selected Line:" );
    //     NodeGraphicsItem::ControlPoint * c_pnt1 = new NodeGraphicsItem::ControlPoint();
    //QLineF l= line_item->QGraphicsLineItem::line();

    line_item->setFlag( QGraphicsItem::ItemIsSelectable, true );
    /*
    QRectF rctf( l.p1(), QSize(10, 10) );
    rctf.moveCenter( l.p1() );
    QRectF rctf1( l.p2(), QSize(10, 10) );
    rctf1.moveCenter( l.p2() );

    QGraphicsRectItem * rect = new QGraphicsRectItem( rctf );
    NodeHandle * rctf_handle = new NodeHandle;
    NodeHandle * rctf1_handle = new NodeHandle;
    QGraphicsRectItem * rect1 = new QGraphicsRectItem( rctf1 );
    QList<QGraphicsItem *> list;
    list << rect << rect1;
    scene->insert( "connection inserted", list );
    */
    //scene->addItem( rect );
  }
 
  //mainWindow->console()->message( "selectItem Line:" );
  std::cout << "SimpleDesigner::selectItem end" << std::endl;
}

void SimpleDesigner::itemsSelected(GraphicsScene * scene, const QList<QGraphicsItem*>& items, QPointF point, Qt::KeyboardModifiers modifiers)
{
  std::cout << "SimpleDesigner::itemsSelected" << std::endl;

  if (!scene) return;
	
  QList<QGraphicsItem*> nodeItems;
  NodeGraphicsItem * node = 0;
	
  selectedItems << items;
  /*
  for( int i=0; i < selectedItems.size()-1; ++i ){
    //    QGraphicsLineItem * lineitem = qgraphicsitem_cast<QGraphicsLineItem*>(selectedItems[i]);
    QGraphicsLineItem * lineitem = qgraphicsitem_cast<QGraphicsLineItem*>(items[i]);
    if( lineitem ){
      mainWindow->console()->message( "SimpleDesigner::itemsSelected:items selected Line:" );
    }
    mainWindow->console()->message( "SimpleDesigner::itemsSelected::for ras" );
  }
  */
  for (int i=0; i < selectedItems.size(); ++i) {
    deselectItem(scene,selectedItems[i]);
		
    if (mode == 2) {
      node = NodeGraphicsItem::cast(selectedItems[i]);
      if (node && !nodeItems.contains(node))
	nodeItems << node;
    }
  }
	
  selectedItems.clear();
	
  if (items.size() != 1 || !items[0]) {
    groupBox1->hide();
    groupBox2->hide();
  }else {
    ItemHandle * handle = getHandle(items[0]);
    if (NodeHandle::cast(handle) && handle->hasNumericalData("concentration")) {
      name1->setText(  handle->name  );
      conc->setText(  QString::number( handle->numericalData("concentration")  ));
      groupBox1->show();
      groupBox2->hide();
    }else if (ConnectionHandle::cast(handle) && handle->hasTextData("rate")) {
      name2->setText(  handle->name  );
      rate->setText(  handle->textData("rate") );
      groupBox1->hide();
      groupBox2->show();
    }else {
      groupBox1->hide();
      groupBox2->hide();
    }
  }
	
  selectedItems = items;
	
  for (int i=0; i < selectedItems.size()-1; ++i) {
    std::cout << "SimpleDesigner::itemsSelected:for selectItem" << std::endl;
    QGraphicsLineItem * lineitem = qgraphicsitem_cast<QGraphicsLineItem*>(selectedItems[i]);
    if( lineitem ){
      mainWindow->console()->message( "SimpleDesigner::itemsSelected: items selected Line:" );
    }
    mainWindow->console()->message( "SimpleDesigner::itemsSelected::for2 ras" );

    selectItem(scene,selectedItems[i]);
    if (mode == 2){
      node = NodeGraphicsItem::cast(selectedItems[i]);
      if (node && !nodeItems.contains(node))
	nodeItems << node;
    }
  }
	
  if (mode == 2 && nodeItems.size() == 2) {	
    QList<NodeGraphicsItem*> list1, list2;
    list1 << NodeGraphicsItem::cast(nodeItems[0]);
    list2 << NodeGraphicsItem::cast(nodeItems[1]);
    ConnectionGraphicsItem * item = new ConnectionGraphicsItem(list1,list2);
    ConnectionHandle * handle = new ConnectionHandle;
    QList<QString> names = scene->networkWindow->symbolsTable.handlesFirstName.keys();
    names += scene->networkWindow->symbolsTable.dataRowsAndCols.keys();

    int i = 1;
    handle->name = tr("J1");

    while (names.contains(handle->name)) {
      ++i;
      handle->name = tr("J") + QString::number(i);
    }
		
    setHandle(item,handle);
    scene->insert(tr("connection inserted"),item);
		
    scene->deselect();
  }
  if( mode == 0 & !items.isEmpty()  ){
    mainWindow->console()->message( "ItemsSelected mode 3:" );
    //    ItemHandle * line_handle = getHandle(items[0]);
    //    QString msg( line_handle->name );
    //    mainWindow->console()->message( msg );
  }else if (items.isEmpty()){
    mainWindow->console()->message( "items empty" );
    QList<QGraphicsItem *> my_sel = scene->selected();
    if( my_sel.isEmpty()){
      mainWindow->console()->message( "sel empty" );
    }else{
      mainWindow->console()->message( "sel non empty" );
    }
  }
}

void SimpleDesigner::escapeSignal(const QWidget * sender)
{
  arrowButton->trigger();
  if (currentScene())
    currentScene()->deselect();
  if( line ) line = NULL;
}

/************ MAIN ******************/

int main(int argc, char *argv[])
{
  PROJECTWEBSITE = QObject::tr("www.tinkercell.com");
  ORGANIZATIONNAME = QObject::tr("Simple Designer");
  PROJECTNAME = QObject::tr("Simple Designer");
	
  ConnectionGraphicsItem::DefaultMiddleItemFile = QString("");
  ConnectionGraphicsItem::DefaultArrowHeadFile = QString(":/images/arrow.xml");
    
  QApplication app(argc, argv);

  QString appDir = QCoreApplication::applicationDirPath();
    
  MainWindow mainWindow(true,true,true,true);
	
  mainWindow.setWindowTitle(QString("Simple Designer"));
  mainWindow.statusBar()->showMessage(QString("Welcome to Simple Designer"));

  Tool * tool = new SimpleDesigner;
  mainWindow.addTool(tool);
	
  GraphicsScene::SelectionRectangleBrush = QBrush(QColor(5,5,5,40));

  mainWindow.newGraphicsWindow();
  mainWindow.newGraphicsWindow();
	
  mainWindow.show();

  int output = app.exec();

  return output;
}
